/**
 * @filename:ScancodeServiceImpl May 7, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.date.DateTime;
import com.wonder.app.domain.IpadUser;
import com.wonder.app.domain.Scan;
import com.wonder.app.domain.Scancode;
import com.wonder.app.mapper.IpadUserDao;
import com.wonder.app.mapper.ScanDao;
import com.wonder.app.mapper.ScancodeDao;
import com.wonder.app.service.IpadUserService;
import com.wonder.app.service.ScanService;
import com.wonder.app.service.ScancodeService;
import com.wonder.app.request.ScancodeRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import io.reactivex.Completable;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(追溯信息服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class ScancodeServiceImpl  extends ServiceImpl<ScancodeDao, Scancode> implements ScancodeService  {

	@Autowired
	private IpadUserDao ipadUserDao;

	@Autowired
	private IpadUserService ipadUserService;

	@Autowired
	private ScancodeDao scancodeDao;

	@Autowired
	private ScanDao scanDao;

	@Autowired
	private ScanService scanService;

	@Autowired
	private ScancodeService scancodeService;
    /**
     * 验证追溯信息信息
     * 
     * @param scancode
     * @param type
     * @return String
     */
    @Override
    public String validate(Scancode scancode,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:追溯信息分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Scancode>   //分页记录
	 */
	@Override 
	public IPage<Scancode> selectList(ScancodeRequest request, Page<Scancode> page){
	    LambdaQueryWrapper<Scancode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Scancode::getId, request.getId());
	      }               
             
        if (StringUtils.hasText(request.getBillNo())) {
			    lambdaQueryWrapper.like(Scancode::getBillNo, request.getBillNo()); 
		    } 
             
        if (request.getCreateTime() != null){
        	Date[] createTime=request.getCreateTime();
        	if(createTime.length>0)
        	  lambdaQueryWrapper.ge(Scancode::getCreateTime, createTime[0]); 
        	if(createTime.length==2)
        	  lambdaQueryWrapper.le(Scancode::getCreateTime,createTime[1]);      
        	
        }    
             
        
		lambdaQueryWrapper.orderByDesc(Scancode::getId);	
		Page<Scancode> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public boolean saveScanCode(Long count) throws Exception{

		List<IpadUser> ipadUsers = ipadUserDao.selectList(new LambdaQueryWrapper<IpadUser>()
				.eq(IpadUser::getTraceability, true));
		for (IpadUser user : ipadUsers){
			user.setTraceability(true);
		}
		//先生成外层表数据
		String billNo = IXqcConst.createBillNo(BillType.ZSM);
		Scancode scancode = new Scancode();
		scancode.setBillNo(billNo);
		scancode.setCreateTime(new DateTime());
		scancode.setCount(count * ipadUsers.size());

		//生成内层表数据
		for (IpadUser user : ipadUsers){
			String userNumber = user.getUserNumber();//员工id
			List<Scan> scanList = new ArrayList<>();//添加到内层的数据集合
			for (int i = 0; i < count; i++){
				String traceabilityCode = userNumber + ",zs" + new Date().getTime();
				Thread.sleep(1);
				Scan scan = new Scan();
				scan.setBillNo(billNo);
				scan.setUserId(userNumber);
				scan.setTrayNum(traceabilityCode);
				scan.setEnable(false);
				scanList.add(scan);
			}
			boolean res = scanService.saveBatch(scanList);
		}

		//添加外层表数据
		boolean save = scancodeService.save(scancode);

		return save;
	}

	@Override
	public Map<String,Object> getDetail(String billNo) {
		List<Scan> scanList = scanDao.selectList(new LambdaQueryWrapper<Scan>().eq(Scan::getBillNo, billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",scanList);
		return data;
	}


}